#include "session_tree_view.h"

CSessionTreeView::CSessionTreeView()
{
    initPopMenu();
    connect(this,SIGNAL(customContextMenuRequested(const QPoint &)),this, SLOT(slotCustomContextMenu(const QPoint &)));
}

CSessionTreeView::~CSessionTreeView()
{
    SAFE_DELETE_ELE(m_addAction);
    SAFE_DELETE_ELE(m_delAction);
}

void CSessionTreeView::initPopMenu()
{
    m_addAction = new QAction(&m_menu);
    m_addAction ->setText(tr("add"));
    m_delAction = new QAction(&m_menu);
    m_delAction ->setText(tr("del"));
    m_menu.addAction(m_addAction );
    m_menu.addAction(m_delAction );
    connect(m_addAction,SIGNAL(triggered()),this,SLOT(slotAddTreeItem()));
    connect(m_delAction,SIGNAL(triggered()),this,SLOT(soltDelTreeItem()));

}

void CSessionTreeView::slotCustomContextMenu(const QPoint &point)
{
    QModelIndex index = indexAt( point );    //取出坐标点处的modelindex
          if( !index.isValid() )
              return;
    m_menu.exec(this->mapToGlobal(point)/*全局位置*/);
}

void CSessionTreeView::slotAddTreeItem()
{

    qDebug()<<"slotAddTreeItem";
    QModelIndex curIndex = this->currentIndex();
    if( !curIndex.isValid() )
              return;
    QModelIndexList selectLists = this->selectedIndexes();

    // 在用户未选中数据的时候在顶层添加数据内容
    if(selectLists.size() == 0){
              CSessionTreeItem item;
              item.setData("name");
           static_cast<CSessionTreeViewModel *>(this->model())->insertItem(item,QModelIndex());
    }
    // 当用户选择的节点大于1的时候不允许插入
    if(selectLists.size() > 1){
            qDebug()<<"不能同时插入大于1";
           return;
    }
    CSessionTreeItem item;
    item.setData("name");
   static_cast<CSessionTreeViewModel *>(this->model())->insertItem(item,curIndex);
    reset();

}

void CSessionTreeView::soltDelTreeItem()
{
    qDebug()<<"soltDelTreeItem";

    //1.给出警示框告知会删除
    //2.用户点击确定删除数据。
    //3.删除对应的配置文件。
    QModelIndex curIndex = this->currentIndex();
    if( !curIndex.isValid() )
            return;

   static_cast<CSessionTreeViewModel *>(this->model())->delTiem(curIndex);
}
